import archiveSites from './archiveSites/index';

function getCurrentUrl() {
  return new Promise(
    (resolve) => {
      browser.tabs.query(
        {active: true}, (tabs) => {
          resolve(tabs[0].url);
        });
    });
}


function showNotification(message) {
  let showNotifications = true; // TODO: pull from settings
  if (!showNotifications) {
    return;
  }
  browser.notifications.create({
    "type": "basic",
    "iconUrl": "icon.svg",
    "title": "#FakeNews Buster",
    "message": message
  });
}

function popupArchiveButtonOnclick() {
  let configuredSites = ['archive.org', 'archive.is']; // TODO: pull from settings
  getCurrentUrl().then((currentUrl) => {
    // TODO: filter based on whitelist and blacklist
    archiveSites(currentUrl, configuredSites).then(() => {
      showNotification("Page has been saved.");
    });
  });
}

window.addEventListener("load", function () {
  document.getElementById('popupArchiveButtonOnclick').onclick = popupArchiveButtonOnclick;
});
